package com.ruoyi.yljf.controller.AppController;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.security.annotation.Anonymous;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.yljf.domain.*;
import com.ruoyi.yljf.domain.dto.NoticeDTO;
import com.ruoyi.yljf.domain.dto.NoticeDTO2;
import com.ruoyi.yljf.domain.enums.yljfEnums;
import com.ruoyi.yljf.domain.vo.*;
import com.ruoyi.yljf.service.INoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 笔记Controller
 * 
 * @author metaee
 * @date 2025-01-04
 */
@RestController
@RequestMapping("api/notice")
public class ApiNoticeController extends BaseController {
    @Autowired
    private INoticeService noticeService;

    @Resource
    private TokenService tokenService;
    /**
     * 查询笔记列表
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(Notice notice) {
        startPage();
        List<Notice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 课程详情-笔记列表
     */
    @Anonymous
    @GetMapping()
    public TableDataInfo getNotice( Long contentId, Long contentPos, String orderBy,HttpServletRequest req) {
        if(orderBy == null){
            LoginUser loginUser = tokenService.getLoginUser(req);
            if(loginUser == null){
                return getDataTable(new ArrayList<>());
            }
        }
        startPage();
        List<CourseNotice> list = noticeService.getNoticeList(contentId, contentPos, orderBy);
        List<NoticeVO> noticeVOS = BeanUtil.copyToList(list, NoticeVO.class);
        return getDataTableInfo(noticeVOS,list);
    }

    /**
     * 课程详情-笔记列表
     */
    @Anonymous
    @GetMapping("/noticeList")
    public TableDataInfo getNotice( Long curId, String noticeType, String orderBy,HttpServletRequest req) {
        if(orderBy == null){
            LoginUser loginUser = tokenService.getLoginUser(req);
            if(loginUser == null){
                return getDataTable(new ArrayList<>());
            }
        }
        startPage();
        List list = noticeService.getNoticeList(curId, noticeType, orderBy);
        List<NoticeVO> noticeVOS = BeanUtil.copyToList(list, NoticeVO.class);
        if(orderBy == null){
            for (NoticeVO c:noticeVOS
            ) {
                Long userId = SecurityUtils.getUserId();
                // 2.判断当前登录用户是否已经点赞
                c.setLiked(noticeService.isLiked(yljfEnums.codeOfDes(noticeType), c.getId(),userId));
            }
        }
        return getDataTableInfo(noticeVOS,list);
    }

    /**
     * 笔记详情
     */
    @Anonymous
    @GetMapping(value = "/noticeDetail")
    public AjaxResult getNoticeDetail(Long noticeId,String noticeType,HttpServletRequest req) {
        Object notice = noticeService.getById(noticeId,noticeType,req);
        return success(notice);
    }

    /**
     * 笔记点赞
     * @param noticeId  笔记id
     * @param noticeType   笔记类型
     * @param req
     * @return 返回1：点赞成功；返回0：取消点赞成功
     */
    @PostMapping(value = "/like")
    public AjaxResult like(Long noticeId, String noticeType,HttpServletRequest req) {
        return success(noticeService.pointLike(noticeId,tokenService.getLoginUser(req).getUserId(),noticeType));
    }


    /**
     * 课程详情-发布课程笔记
     */
    @PostMapping(value = "/addNotice")
    public AjaxResult addNotice(@RequestBody NoticeDTO noticeDTO) {
        Integer count = noticeService.addNotice(noticeDTO);
        return success(count);
    }

    /**
     * 课程详情-发布经方/医案/药材笔记
     */
    @PostMapping(value = "/addNoticeV2")
    public AjaxResult addNoticeV2(@RequestBody NoticeDTO2 noticeDTO) {
        Integer count = noticeService.addNoticeV2(noticeDTO);
        return success(count);
    }


    /**
     * 心得笔记-课程笔记
     */
    @Anonymous
    @GetMapping("/courseNotice")
    public TableDataInfo getCourseNotice() {
        startPage();
        List<CourseNotice> list = noticeService.getCourseNotice();
        List<NoticeVO2> noticeVO2s = BeanUtil.copyToList(list, NoticeVO2.class);
        for (NoticeVO2 n:noticeVO2s
        ) {
            n.setNoticeType(yljfEnums.NOTICE_TYPE_COURSE.getDesc());
        }
        return getDataTableInfo(noticeVO2s,list);
    }

    /**
     * 心得笔记-经方笔记
     */
    @Anonymous
    @GetMapping("/jingfangNotice")
    public TableDataInfo getJingfangNotice() {
        startPage();
        List<JingfangNotice> list = noticeService.getJingfangNotice();
        List<NoticeVO2> noticeVO2s = BeanUtil.copyToList(list, NoticeVO2.class);
        for (NoticeVO2 n:noticeVO2s
        ) {
            n.setNoticeType(yljfEnums.NOTICE_TYPE_JINGFANG.getDesc());
        }
        return getDataTableInfo(noticeVO2s,list);
    }
    /**
     * 心得笔记-药材笔记
     */
    @Anonymous
    @GetMapping("/drugNotice")
    public TableDataInfo getDrugNotice() {
        startPage();
        List<DrugNotice> list = noticeService.getDrugNotice();
        List<NoticeVO2> noticeVO2s = BeanUtil.copyToList(list, NoticeVO2.class);
        for (NoticeVO2 n:noticeVO2s
        ) {
            n.setNoticeType(yljfEnums.NOTICE_TYPE_DRUG.getDesc());
        }
        return getDataTableInfo(noticeVO2s,list);
    }
    /**
     * 心得笔记-医案笔记
     */
    @Anonymous
    @GetMapping("/medicalNotice")
    public TableDataInfo getMedicalNotice() {
        startPage();
        List<MedicalNotice> list = noticeService.getMedicalNotice();
        List<NoticeVO2> noticeVO2s = BeanUtil.copyToList(list, NoticeVO2.class);
        for (NoticeVO2 n:noticeVO2s
        ) {
            n.setNoticeType(yljfEnums.NOTICE_TYPE_MEDIAL.getDesc());
        }
        return getDataTableInfo(noticeVO2s,list);
    }

    /**
     * 课程详情-保存课程笔记
     */
    @PostMapping(value = "/saveNotice")
    public AjaxResult saveNotice(Long id, String noticeType) {
        if(noticeType.equals("课程"))
            return success(noticeService.saveNotice(id));
        else
            return success(noticeService.saveNoticeV2(id));
    }

//    /**
//     * TODO 课程详情-保存经方/医案/药材笔记
//     */
//    @PostMapping(value = "/saveNoticeV2")
//    public AjaxResult saveNoticeV2( Long noticeId, String noticeType) {
//        Integer count = noticeService.saveNoticeV2(noticeId,noticeType);
//        return success(count);
//    }

}
